package easyzhou.com.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

import easyzhou.com.bean.Result;
import easyzhou.com.config.InitConfig;
import easyzhou.com.service.impl.DefalutIncermentalPageBuilderImpl;
import easyzhou.com.utils.ResultFormater;

public class NorthPanel extends JPanel {
	/**
		 * 
		 */
	private static final long serialVersionUID = 1923159046971200540L;
	private JButton goo;
	private JButton localProjectChooserButton;
	private JButton outputChooserButton;
	private JTextField localProjectField;
	private JTextField outputField;
	private MainFrame parent;

	private NorthPanel(MainFrame parent) {
		this.parent = parent;
	}

	public static final NorthPanel newInstance(MainFrame frame) {
		NorthPanel panel = new NorthPanel(frame); // 创建一个JPanel的实例
		panel.init();
		return panel;
	}

	public void init() {
		setBackground(Color.yellow); // 将JPanel的实例背景设置为黄色
		GridBagLayout gridLayout = new GridBagLayout();
		setLayout(gridLayout);

		goo = new JButton("Goo");
		goo.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				String centerTextAreaText = parent.getCenterPanel().getCenterTextAreaText();
				String outputDir = outputField.getText();
				String localProjec = localProjectField.getText();
				DefalutIncermentalPageBuilderImpl builder = new DefalutIncermentalPageBuilderImpl(localProjec,
						outputDir);
				Result result = builder.builde(centerTextAreaText);
				String str = ResultFormater.toString(result);

				parent.getCenterPanel().setCenterTextAreaText(str);
				System.out.println(result);

			}

		});

		localProjectField = new JTextField("本地项目");
		localProjectField.setSize(150, 30);
		localProjectField.setText(InitConfig.getDefaultProductPath());
		localProjectField.enable(false);

		localProjectChooserButton = new JButton("本地项目");

		localProjectChooserButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

				int returnValue = fileChooser.showOpenDialog(parent);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					localProjectField.setText(fileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});

		outputField = new JTextField("输出位置");
		outputField.setSize(150, 30);
		outputField.enable(false);
		outputField.setText(InitConfig.getDefaultOutPutPath());

		outputChooserButton = new JButton("输出位置");
		outputChooserButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnValue = fileChooser.showOpenDialog(parent);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					outputField.setText(fileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});

		initLayout(gridLayout);

		add(localProjectField);
		add(localProjectChooserButton);
		add(outputField);
		add(outputChooserButton);

		add(goo);

	}

	private void initLayout(GridBagLayout gridLayout) {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		// 是用来控制添加进的组件的显示位置
		gridBagConstraints.fill = gridBagConstraints.BOTH;
		// 该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况
		// NONE：不调整组件大小。
		// HORIZONTAL：加宽组件，使它在水平方向上填满其显示区域，但是不改变高度。
		// VERTICAL：加高组件，使它在垂直方向上填满其显示区域，但是不改变宽度。
		// BOTH：使组件完全填满其显示区域。
		{
			gridBagConstraints.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints.gridwidth = 3;// 该方法是设置组件水平所占用的格子数，如果为0，就说明该组件是该行的最后一个
			gridBagConstraints.weightx = 3;// 第一列的分布方式为10%
			gridBagConstraints.weighty = 1;// 该方法设置组件垂直的拉伸幅度，如果为0就说明不拉伸，不为0就随着窗口增大进行拉伸，0到1之间
			gridLayout.setConstraints(localProjectField, gridBagConstraints);// 设置组件
		}
		{

			gridBagConstraints.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.weightx = 1;
			gridBagConstraints.weighty = 1;
			gridLayout.setConstraints(localProjectChooserButton, gridBagConstraints);// 设置组件
		}
		{
			gridBagConstraints.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints.gridwidth = 3;
			gridBagConstraints.weightx = 3;
			gridBagConstraints.weighty = 1;
			gridLayout.setConstraints(outputField, gridBagConstraints);// 设置组件
		}
		{
			gridBagConstraints.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.weightx = 1;
			gridBagConstraints.weighty = 1;
			gridLayout.setConstraints(outputChooserButton, gridBagConstraints);// 设置组件
		}
		{
			gridBagConstraints.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.weightx = 1;
			gridBagConstraints.weighty = 1;
			gridLayout.setConstraints(goo, gridBagConstraints);// 设置组件
		}
	}
}
